<?php

/**
 * Description of imagenes_propiedades
 *
 * @author daniel
 */
class Imagenes_propiedades extends Model {


	function Imagenes_propiedades() {
		parent::Model();
		$this->load->database();
		$this->load->library('image_lib');
	}

	function agregar_imagen($id_propiedad, $name = '') {
		//Get File Data Info
		$uploads = array($this->upload->data());

		//Move Files To User Folder
		foreach ($uploads as $key[] => $value) {
				$thumb_extension = "_tn";
				//Gen Random code for new file name
				$randomcode = $this->generate_code(12);
				$newimagename = $randomcode . $value['file_ext'];
				$thumbimagename = $randomcode . $thumb_extension . $value['file_ext'];

				$file_extension = $value["file_ext"];
				$original_file_name = $value["full_path"];

				$new_file_name = substr($original_file_name, 0, strrpos($original_file_name, "/"));
				$thumb_file_name = substr($original_file_name, 0, strrpos($original_file_name, "/"));

				$new_file_name .= "/" . $randomcode . $file_extension;
				$thumb_file_name .= "/" . $randomcode . $thumb_extension . $file_extension;

				rename($original_file_name,  $new_file_name);


				$config = array();
				$config['image_library'] = 'GD2';
				$config['source_image'] = $new_file_name;
				$config['create_thumb'] = TRUE;
				$config['maintain_ratio'] = FALSE;
				$config['width'] = 177;
				$config['height'] = 100;
				$config['new_image'] = $thumbimagename;

				$this->image_lib->clear();
				$this->image_lib->initialize($config);

				if (!$this->image_lib->resize()) {
						echo $this->image_lib->display_errors();
				}


				//Make Some Variables for Database
				$imagename = $newimagename;
				$thumbnail = $thumbimagename;
				$filesize = $value['file_size'];
				$width = $value['image_width'];
				$height = $value['image_height'];
				
				if( $name == 'edit_foto_principal'){

						$query = $this->db->query("select * from  imagenes_propiedades where id in ( SELECT min(id) FROM imagenes_propiedades where id_propiedad = $id_propiedad )");

						foreach ($query->result_array() as $row)
						{
								$this->db->set('id_propiedad', $row['id_propiedad']);
								$this->db->set('nombre', $row['nombre']);
								$this->db->set('thumb', $row['thumb']);
								$this->db->set('tamanho', $row['tamanho']);
								$this->db->set('ancho', $row['ancho']);
								$this->db->set('alto', $row['alto']);
								$this->db->insert('imagenes_propiedades');

								$data = array(
										'id_propiedad' => $id_propiedad,
										'nombre' => $imagename,
										'thumb' => $thumbnail,
										'tamanho' => $filesize,
										'ancho' => $width,
										'alto' => $height
								);

								$this->db->where('id', $row['id']);
								$this->db->update('imagenes_propiedades', $data);
						}

				}else{
						
						//Add Pic Info To Database
						$this->db->set('id_propiedad', $id_propiedad);
						$this->db->set('nombre', $imagename);
						$this->db->set('thumb', $thumbnail);
						$this->db->set('tamanho', $filesize);
						$this->db->set('ancho', $width);
						$this->db->set('alto', $height);
						$this->db->insert('imagenes_propiedades');


				}
				
		}
	}


	function generate_code($length = 10) {
		if ($length <= 0) {
			return false;
		}

		$code = "";
		$chars = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";

		srand((double) microtime() * 1000000);

		for ($i = 0; $i < $length; $i++) {
			$code = $code . substr($chars, rand() % strlen($chars), 1);
		}

		return $code;
	}

  function eliminar_imagenes($id) {
		$this->db->delete('imagenes_propiedades', array('id_propiedad' => $id));
	}

  function eliminar_imagenes_by_id($id) {
		$this->db->delete('imagenes_propiedades', array('id' => $id));
	}

}

?>
